package com.ecloud.trend.controller;

import com.ecloud.common.JsonResult;
import com.ecloud.common.ResultEnum;
import com.ecloud.trend.dto.TrendSectionDto;
import com.ecloud.trend.entities.TdbDaily;
import com.ecloud.trend.service.TdbService;
import com.ecloud.trend.vo.*;
import com.ecloud.trend.service.TrendService;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * TrendController
 *
 * @author Squbi
 * @date 2021/6/18 15:37
 */
@Api(tags = "TrendDB的操作接口")
@RestController
@RequiredArgsConstructor
public class TrendController {

    private final TdbService tdbService;
    private final TrendService trendService;

    @ApiOperation("测试链接")
    @GetMapping("/trend/testConn")
    public JsonResult test() {
        boolean b = trendService.testConn();
        return b ? JsonResult.success() : JsonResult.failed(ResultEnum.DB_CONN_ERROR);
    }

    @ApiOperation("获取标签对应的历史数据时间定位")
    @ApiImplicitParam(name = "tag", value = "标签", required = true)
    @GetMapping("/trend/histPosition/{tag}")
    public JsonResult histPosition(@PathVariable("tag") String tag) {
        TrendHistPositionVo trendHistPositionVo = trendService.histPosition(tag);
        return JsonResult.success(trendHistPositionVo);
    }

    @ApiOperation("获取标签对应的历史状态")
    @ApiImplicitParam(name = "tag", value = "标签", required = true)
    @GetMapping("/trend/histStatus/{tag}")
    public JsonResult histStatus(@PathVariable("tag") String tag) {
        TrendHistStatusVo trendHistStatusVo = trendService.histStatus(tag);
        return JsonResult.success(trendHistStatusVo);
    }

    @ApiOperation("获取标签对应的所有数据")
    @ApiImplicitParam(name = "tag", value = "标签", required = true)
    @GetMapping("/trend/trendAll/{tag}")
    public JsonResult trendAll(@PathVariable("tag") String tag) {
        List<TrendDataVo> trendDataVos = trendService.trendAll(tag);
        return JsonResult.success(trendDataVos);
    }

    @ApiOperation("获取标签对应的最新数据")
    @ApiImplicitParam(name = "tag", value = "标签", required = true)
    @GetMapping("/trend/last/{tag}")
    public JsonResult lastValue(@PathVariable("tag") String tag) {
        List<TrendDataVo> trendDataVos = trendService.lastValue(tag);
        return JsonResult.success(trendDataVos);
    }

    @ApiOperation("批量获取标签对应的最新数据")
    @ApiImplicitParam(name = "tags", value = "标签集合", required = true)
    @PostMapping("/trend/last")
    public JsonResult lastValues(@RequestBody String[] tags) {
        List<TrendDataVo> trendDataVos = trendService.lastValues(tags);
        return JsonResult.success(trendDataVos);
    }

    @ApiOperation("根据时间获取标签数据")
    @ApiImplicitParam(name = "sectionDto", value = "时间定位", required = true)
    @PostMapping("/trend/section")
    public JsonResult sectionData(@RequestBody TrendSectionDto sectionDto) {
        List<TrendDataVo> trendDataVos = trendService.sectionData(sectionDto);
        return JsonResult.success(trendDataVos);
    }

    @ApiOperation("昨日的统计数据")
    @GetMapping("/tdb/yesterday")
    public JsonResult tdbYesterday() {
        List<TdbDaily> yesterday = tdbService.yesterday();
        return JsonResult.success(yesterday);
    }

    @ApiOperation("获取实时能量数据")
    @GetMapping("/tdb/present/energy")
    public JsonResult presentEnergy() {
        List<TdbPresentVo> tdbPresentVos = tdbService.selectPresentEnergyData();
        return JsonResult.success(tdbPresentVos);
    }

    @ApiOperation("获取实时控制数据")
    @GetMapping("/tdb/present/control")
    public JsonResult presentControl() {
        List<TdbPresentVo> tdbPresentVos = tdbService.selectPresentControlData();
        return JsonResult.success(tdbPresentVos);
    }

    @ApiOperation("获取实时环境数据")
    @GetMapping("/tdb/present/environment")
    public JsonResult presentEnvironment() {
        TdbEnvironmentPresentResult result = tdbService.presentEnvironmentData();
        return JsonResult.success(result);
    }
}
